package com.yilian.xlmusic.android.utils.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yilian.xlmusic.android.R;
import com.yilian.xlmusic.android.utils.MeasureUnitTranUtil;

public class SearchLayout extends RelativeLayout {
    private EditText editText;
    private TextView back;
    private RelativeLayout foot;
    private ImageView close;
    public SearchLayout(Context context) {
        this(context, null);
    }

    public SearchLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs,defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View.inflate(context, R.layout.view_searchlayout,this);
        foot = this.findViewById(R.id.search_foot);
        editText = this.findViewById(R.id.search_text);
        close = this.findViewById(R.id.search_close);
        back = this.findViewById(R.id.search_back);

        initListener(context);
    }

    private void initListener(Context context) {
        close.setOnClickListener(v -> editText.setText(""));
        back.setOnClickListener(v -> {

        });
        editText.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                DisplayMetrics dm = new DisplayMetrics();
                wm.getDefaultDisplay().getMetrics(dm);
                int width = dm.widthPixels;         // 屏幕宽度（像素）
                //int height = dm.heightPixels;       // 屏幕高度（像素）
                float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
                //int densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）
                // 屏幕宽度算法:屏幕宽度（像素）/屏幕密度
                int screenWidth = (int) (10*density)*2;  // 根据xml中边距10dp计算px
                //int screenWidth = (int) (width / density);  // 屏幕宽度(dp)

                //int screenHeight = (int) (height / density);// 屏幕高度(dp)

                //为RelativeLayout设置宽高
                ViewGroup.LayoutParams params = foot.getLayoutParams();
                if (hasFocus){
                    params.width = width- MeasureUnitTranUtil.dip2px(context,45);//单位必须是px
                }else {
                    params.width = width;
                }
                foot.setLayoutParams(params);
            }
        });
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}
